package com.base.cn.platform.os.controller.mobile.course.record;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.cou.record.CourseLearnFlowService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * 播放记录Controller
 *
 * @author s.li
 * @create 2018-08-07-15:39
 */
@Controller
@RequestMapping("/mobile/uc/course/play/record")
public class MobileCoursePlayRecordController extends MobileBaseController {

    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private CourseLearnFlowService courseLearnFlowService;
    @Autowired
    private LoadStartExamService loadStartExamService;

    /**
     * 保存课程播放记录
     *
     * @param playRecord 课程播放记录
     */
    @RequestMapping("/savePlayRecord")
    @ResponseBody
    public Map<String, Object> savePlayRecord(HttpServletRequest request,
                                              @RequestBody Map<String, Object> playRecord) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        playRecord.put("userId", userId);
        playRecord.put("source", 2);
        return coursePlayRecordService.saveCoursePlayRecord(playRecord);
    }

    /**
     * 加载用户学习记录列表
     * @return
     */
    @RequestMapping("/userStudyRecord")
    public String userStudyRecord(Model model,@RequestParam("id") BigDecimal courseId,
                                  @RequestParam("courseType") String  courseType){
        model.addAttribute("courseId",courseId);
        model.addAttribute("courseType",courseType);
        return resultPage("/course/play/record/user-study-record");
    }

    /**
     * 加载数据列表
     * @param request
     * @param model
     * @param params
     * @param currentPage
     * @return 操作页面
     */
    @RequestMapping("/ajaxUserStudyRecord")
    public String ajaxUserStudyRecord(HttpServletRequest request,
                                     Model model,
                                     @RequestBody Map<String, Object> params,
                                     @RequestParam("currentPage") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        Map<String,Object> dataMap = courseLearnFlowService.findCoursePlayRecordPracticeFlowPage(params,currentPage,10);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/play/record/ajax-user-study-record");
    }

    /**
     * 用户随堂练习结果
     */
    @RequestMapping("/userExercisesRecord")
    public String userExercisesRecord(HttpServletRequest request,
                                      Model model, @RequestParam BigDecimal examRecordId,@RequestParam BigDecimal courseId,@RequestParam BigDecimal catalogId,@RequestParam double scorePercent,@RequestParam boolean pass,
                                      @RequestParam BigDecimal byCourseId){

        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",examRecordId);
        condition.put("userId",userId);
        condition.put("loreSetPointData",true);
        condition.put("rightNumData",true);
        Map<String,Object> dataMap = loadStartExamService.loadExamRecord(condition);
        if(ResultUtil.getSuccess(dataMap)){
            model.addAttribute("examRecord",ResultUtil.getEntity(dataMap));
            Map<String,Object> cardMap = loadStartExamService.loadExamRecordCard(userId,examRecordId,0);
            model.addAttribute("cardMap",cardMap);
        }
        model.addAttribute("courseId",courseId);
        model.addAttribute("catalogId",catalogId);
        model.addAttribute("byCourseId",byCourseId);
        model.addAttribute("scorePercent",scorePercent);
        model.addAttribute("pass",pass);
        return resultPage("/course/play/record/user-exercises-record");
    }

}
